<?php

defined('SYSPATH') or die('Access error!');

class Model_Shop_Store extends Model
{

    public $rules = array(
        'name' => array
            (
            'not_empty' => null,
        ),
        'description' => array(
            'not_empty' => null,
        )
    );

    /**
     *  根据用户的id获取某个店铺的详细内容
     * @param type $user_id
     */
    public function get_store_by_userid($user_id)
    {
        $store_detail = $this->get_row(array('user_id' => $user_id));
        if (!empty($store_detail['store_logo']))
        {
            $store_detail['store_logo'] = $this->create_store_logo($store_detail['store_logo'], Session::instance()->get_detail('user', 'id'));
            $store_detail['store_banner'] = $this->create_store_logo($store_detail['store_banner'], Session::instance()->get_detail('user', 'id'));
        }
        return Arr::validate_arr($store_detail) ? $store_detail : array();
    }

    /**
     *  根据店铺的logo和用户的id生成店铺的logo
     * @param type $logo
     * @param type $user_id
     */
    public function create_store_logo($logo, $user_id)
    {
        return SHOP_STORE_IMAGES_FILE_PATH . '/' . $user_id . '/' . $logo;
    }

    public function create_store_url($store_id)
    {
        return URL::site(Route::get('store')->uri(array('action' => 'index', 'id' => $store_id)));
    }

    /**
     *  根据店铺的id获取某个店铺的详细内容
     * @param type $user_id
     */
    public function get_store_by_id($store_id)
    {
        $store_detail = $this->get_row(array('id' => $store_id));
        if (!empty($store_detail['store_logo']))
        {
            $store_detail['store_logo'] = SHOP_STORE_IMAGES_FILE_PATH . '/' . $store_detail['user_id'] . '/' . $store_detail['store_logo'];
            $store_detail['store_banner'] = SHOP_STORE_IMAGES_FILE_PATH . '/' . $store_detail['user_id'] . '/' . $store_detail['store_banner'];
        }
        return Arr::validate_arr($store_detail) ? $store_detail : array();
    }

    /**
     * 获取推荐店铺
     */
    public function get_recommend_store()
    {
        $recommend_store_temp = $this->get_results(' and recommend=1', 'id,user_id,name,store_logo,theme');
        $recommend_store = array();
        if (Arr::validate_arr($recommend_store_temp))
        {
            foreach ($recommend_store_temp as $key => $value)
            {
                $value['store_logo'] = SHOP_STORE_IMAGES_FILE_PATH . '/' . $value['user_id'] . '/' . $value['store_logo'];
                $value['store_url'] = $this->create_store_url($value['id']);
                $recommend_store[] = $value;
            }
        }
        unset($recommend_store_temp);
        return $recommend_store;
    }

    /**
     * 获得模版的信息
     *
     * @access  private
     * @param   string      $template_name      模版名
     * @return  array
     */
    public function get_themes_info($template_name)
    {
        $info = array();
        $info['code'] = $template_name;
        $info['screenshot'] = '';
        if (file_exists(SHOP_TEMPLATE_STYLE . $template_name . "/screen.png"))
        {
            $info['screen'] = SHOP_TEMPLATE_SCREEN . '/' . $template_name . "/screen.png";
        }
        return $info;
    }
    
    

}